import os.path
import json

import pandas as pd
import datetime
from datetime import timezone


def timestamp_to_utc_str(timestamp, fmt="%Y-%m-%d %H:%M:%S"):
    """
    将时间戳转换为UTC时间字符串

    参数:
        timestamp: Unix时间戳（秒级）=]
        fmt: 输出字符串的格式
    返回:
        str: UTC时间字符串
    """
    # 创建UTC时间对象
    utc_dt = datetime.datetime.fromtimestamp(timestamp, tz=timezone.utc)

    # 格式化为字符串
    return utc_dt.strftime(fmt)+"+00:00"


base_path = os.path.dirname(os.path.dirname(__file__))
with open(f"{base_path}/json/price_usd_ohlc.json") as fp:
    json_data = json.load(fp)


ret_list = []
for x in json_data:
    tmp_dict = x['o']
    tmp_dict['t'] = x['t']
    ret_list.append(tmp_dict)
ret_df = pd.DataFrame(ret_list)
ret_df["datetime"] = ret_df["t"].apply(lambda x: timestamp_to_utc_str(x))
print("stop")